﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;
using Amkeni_System.Classes;
using Amkeni_System.Report;

namespace Amkeni_System
{
    public partial class Archived_Project_Reports : PDFCreator
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ArchivedProjectsGridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "pdf")
            {
                //Get the selected row
                int currentRowIndex = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = ArchivedProjectsGridView2.Rows[currentRowIndex];

                //Get the Datakeynames for the selected row
                string selPROJ = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["ip_id"].ToString();
                string selYr = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["year_id"].ToString();
                string selRPRD = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["report_period_id"].ToString();
                string theRFP = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["RFP_id"].ToString();

                int proj = int.Parse(selPROJ);
                int reportp = int.Parse(selRPRD);
                int year = int.Parse(selYr);
                int rfp = int.Parse(theRFP);

                //PdfCreator NewPdfDoc = new PdfCreator();
                //NewPdfDoc
                CreatePDF(proj, reportp, year, rfp);
            }
        }

        protected void ArchivedProjectsGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Get the selected row
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = ArchivedProjectsGridView1.Rows[currentRowIndex];

            //Get the Datakeynames for the selected row
            string selPROJ = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["ip_id"].ToString();
            string selYr = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["year_id"].ToString();
            string selRPRD = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["report_period_id"].ToString();
            string theRFP = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["RFP_id"].ToString();

            int proj = int.Parse(selPROJ);
            int reportp = int.Parse(selRPRD);
            int year = int.Parse(selYr);
            int rfp = int.Parse(theRFP);

            if (e.CommandName == "pdf")
            {
                //NewPdfDoc
                CreatePDF(proj, reportp, year, rfp);
            }
            else if (e.CommandName == "return")
            {
                try
                {
                    ip_report pr = db.ip_report.SingleOrDefault(
                                            mypr => mypr.ip_id == proj &&
                                            mypr.year_id == year &&
                                            mypr.report_period_id == reportp &&
                                            mypr.RFP_id == rfp);

                    pr.archived = false;
                    pr.approved = true;

                    db.SaveChanges();
                    ArchivedProjectsGridView1.DataBind();
                }
                catch (Exception ex)
                {
                    Error.Text = "Error : " + ex.Message + " : " + ex.StackTrace;
                    Error.Visible = true;
                }
            }
        }
    }
}